/**
 * Created by zhouli on 18/4/25
 * Email li.zhou@huilianyi.com
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';

export default {
  //获取角色列表
  getRoleUserListByRoleName(roleName, params) {
    // let params = {
    //   page: "page",
    //   size: 10,
    // }
    // roleName ROLE_COMPANY_FINANCE_RECEIVED 收单人
    // roleName ROLE_TENANT_ADMIN 集团管理员
    // roleName ROLE_COMPANY_ADMIN 公司管理员
    // roleName ROLE_COMPANY_BOOK_ADMIN 订票管理员
    // roleName ROLE_COMPANY_BOOK 订票专员
    // roleName ROLE_DATA_ADMIN 数据专员

    return new Promise(function (resolve, reject) {
      httpFetch.get(config.baseUrl + '/api/account/get/users/' + roleName, params)
        .then(function (res) {
          resolve(res)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //添加某种角色的人
  addRoleUser(params) {
    // let params = {
    //   authorityName:"ROLE_COMPANY_FINANCE_RECEIVED",
    //   userOIDs:[]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(config.baseUrl + '/api/account/set/authority/v2', params)
        .then(function (res) {
          resolve(res)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //删除某种角色的人
  deleteRoleUser(params) {
    // let params = {
    //   authorityName:"ROLE_COMPANY_FINANCE_RECEIVED",
    //   userOIDs:[]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.delete(config.baseUrl + '/api/account/remove/authority/v2', params)
        .then(function (res) {
          resolve(res)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //更新收单人
  updateReceiptUserList(params) {
    // let params = {
    //   authorityName:"ROLE_COMPANY_FINANCE_RECEIVED",
    //   userOIDs:[]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.get(config.baseUrl + '/api/account/set/authority', params)
        .then(function (res) {
          resolve(res)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },

  //获取财务角色列表
  getFinanceRoleList(page, size, searchParams) {
    return new Promise(function (resolve, reject) {
      let url = `${config.baseUrl}/api/finance/role/list?page=${page}&size=${size}`;
      for (let searchName in searchParams) {
        url += searchParams[searchName] ? `&${searchName}=${searchParams[searchName]}` : ''
      }
      httpFetch.get(url).then(res => {
        resolve(res)
      }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //查询财务角色编号是否已存在
  judgeFinanceRoleExist(roleID) {
    return httpFetch.get(`${config.baseUrl}/api/finance/role/id/exist?roleID=${roleID}`)
  },

  //新建财务角色
  newFinanceRole(params) {
    // params = {
    //   roleDescription: "角色说明",
    //   roleID: "角色编号",
    //   roleName: "角色名称"
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/create`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //更新财务角色
  updateFinanceRole(params) {
    // params = {
    //   roleOID: "4da8171a-8b67-45bb-8a39-f7ac53548831",
    //   roleName: "角色名称",
    //   roleDescription: "角色说明"
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.put(`${config.baseUrl}/api/finance/role/edit/base`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //删除财务角色
  deleteFinanceRole(roleOID) {
    return new Promise(function (resolve, reject) {
      httpFetch.delete(`${config.baseUrl}/api/finance/role/delete/${roleOID}`)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //财务角色详情
  getFinanceRoleDetail(roleOID) {
    return new Promise(function (resolve, reject) {
      httpFetch.get(`${config.baseUrl}/api/finance/role/detail/${roleOID}`)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //保存财务页面权限配置
  saveFinancePageAuth(params) {
    // params=[
    //   {pageName: '', action: 0, roleOID: ''}
    //     ...
    // ]
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/edit/page`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //更新法人实体
  updateCorporationEntity(params) {
    // params = {
    //   roleOID: "4da8171a-8b67-45bb-8a39-f7ac53548831",
    //   corporationEntityOIDs: ["3a0b2b4f-1b8f-4d49-9346-7b56f9a90cc8", "82bf6f93-8676-4acb-b227-2b523eb20156"]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/edit/organization/corporation`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //更新部门权限
  updateDepartmentAuth(params) {
    // params = {
    //   roleOID: "4da8171a-8b67-45bb-8a39-f7ac53548831",
    //   departmentOIDs: ["90bcef39-6f2c-4fdd-99b0-aaedc2933e5d", "2e66df81-d39e-49fa-a9a7-5857cd69f75f"]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/edit/organization/department`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //获取成本中心权限列表
  getFinanceCostCenter(roleOID) {
    return new Promise(function (resolve, reject) {
      httpFetch.get(`${config.baseUrl}/api/finance/role/cost/centers/${roleOID}`)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //更新成本中心权限列表
  updateFinanceCostCenter(roleOID, params) {
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/cost/centers/${roleOID}`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //搜索成本中心
  searchCostCenter(keyword, page, size) {
    let params = {
      keyword: encodeURIComponent(keyword),
      page,
      size
    }
    return httpFetch.get(`${config.baseUrl}/api/cost/centers/search`, params)
  },

  //搜索成本中心编码
  searchCostCenterCode(costCenterOID, keyword, page, size) {
    let params = {
      keyword: encodeURIComponent(keyword),
      page,
      size
    }
    return httpFetch.get(`${config.baseUrl}/api/cost/center/${costCenterOID}/item/search`,params)
  },

  //获取分配人员列表
  getAssignUsers(roleOID, page, size) {
    let params = {
      page,
      size
    }
    return new Promise(function (resolve, reject) {
      httpFetch.get(`${config.baseUrl}/api/finance/role/get/assign/${roleOID}`,params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //新增分配人员
  createAssignUsers(params) {
    // params = {
    //   roleOID: '',
    //   userOIDs: ['', ...]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/create/assign`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //删除分配人员
  deleteAssignUsers(params) {
    // params = {
    //   roleOID: '',
    //   userOIDs: ['', ...]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/remove/assign`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //新增部门
  createDepartment(params) {
    // params = {
    //   roleOID: '',
    //   departmentOIDs: ['', ...]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/create/organization/department`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

  //删除部门
  deleteDepartment(params) {
    // params = {
    //   roleOID: '',
    //   departmentOIDs: ['', ...]
    // }
    return new Promise(function (resolve, reject) {
      httpFetch.post(`${config.baseUrl}/api/finance/role/remove/organization/department`, params)
        .then(res => {
          resolve(res)
        }).catch(e => {
        errorMessage(e.response);
        reject(e.response)
      })
    })
  },

}
